package com.dingwei.superiordiary.mvp.presenter;

import com.dingwei.superiordiary.base.MyBaseBean;
import com.dingwei.superiordiary.bean.FailsBean;
import com.dingwei.superiordiary.bean.MyAddressBean;
import com.dingwei.superiordiary.custom.myCallback;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.base.URLS;
import com.lihao.baselibrary.base.basePresenter;

import org.xutils.http.RequestParams;
import org.xutils.x;

import java.net.URL;

/**
 * Created by lihao on 2017/9/12.
 */

public class MyAddressPresenter extends basePresenter {

    private Contract.MyAddressView myAddressView;

    public MyAddressPresenter(Contract.MyAddressView view) {
        super(view);
        this.myAddressView = view;
    }

    public void initData() {
        RequestParams rp = new RequestParams(URLS.HOST+URLS.GET_ADDRESS);
        rp.addBodyParameter("sessionid", BaseActivity.value.getToken());

        x.http().post(rp, new myCallback(BaseActivity.context, MyAddressBean.class) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                MyAddressBean bean = (MyAddressBean) baseBean;

                myAddressView.setData(bean);
            }

            @Override
            public void fail(FailsBean baseBean, String result) {
                myAddressView.onFail();
            }
        });
    }

    public void deleteAddress(String id, final int position) {
        RequestParams rp = new RequestParams(URLS.HOST+URLS.DELETE_ADDRESS);
        rp.addBodyParameter("sessionid", BaseActivity.value.getToken());
        rp.addBodyParameter("id",id);

        x.http().post(rp, new myCallback(BaseActivity.context, MyBaseBean.class,rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {

                showToast(baseBean.message);
                myAddressView.deleteSuccess(position);
            }

            @Override
            public void fail(FailsBean baseBean, String result) {
                myAddressView.onFail();
            }
        });
    }

    public void setDefaultAddress(String id, final int position) {
        RequestParams rp = new RequestParams(URLS.HOST+URLS.SET_ADDRESS_DEFAULT);
        rp.addBodyParameter("sessionid", BaseActivity.value.getToken());
        rp.addBodyParameter("id",id);

        x.http().post(rp, new myCallback(BaseActivity.context, MyBaseBean.class) {
            @Override
            public void back(MyBaseBean baseBean, String result) {

                showToast(baseBean.message);
                myAddressView.setDefaultSuccess(position);

            }

            @Override
            public void fail(FailsBean baseBean, String result) {
                myAddressView.onFail();
            }
        });
    }
}
